/* BaseImportAction.java
 * Author: liuex
 * 2007-12-19 ����11:32:50
 *
 * Note: 
 */
package lex.proxyverfier.actions;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.List;

import javax.swing.JFileChooser;

import org.lex.net.proxy.ProxyServer;

import lex.proxyverfier.ProxyVerifier;

public abstract class BaseImportAction extends BaseImportExportAction
{
	protected abstract List<ProxyServer> importProxyServers( BufferedReader in ) throws Exception;

	protected void doInBackground( ActionEvent e )
	{
		JFileChooser fc = this.createFileChooser();
		Window root = ProxyVerifier.getApplication().getRootWindow();
		int opt = fc.showOpenDialog( root );
		if( JFileChooser.APPROVE_OPTION == opt )
		{
			File file = fc.getSelectedFile();
			File workingDirectory = file.getParentFile();
			this.setWorkingDirectory( workingDirectory );
			BufferedReader in = null;
			List<ProxyServer> servers = null;
			try
			{
				in = new BufferedReader( new FileReader( file ) );
				servers = this.importProxyServers( in );
				ImportExportOperations.importProxyServers( servers );
			}
			catch( Exception e1 )
			{
				e1.printStackTrace();
				this.showError( "Error import file", e1.getMessage() );
				return;
			}
		}
	}
}
